package com.atguigu.tingshu.payment.client;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.dto.PaymentDto;
import com.atguigu.tingshu.payment.factory.PayFactory;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.impl.WxPaymentInfoServiceImpl;
import com.github.wxpay.sdk.WXPayUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

@Tag(name = "支付相关内部接口的控制层")
@RestController
@RequestMapping("client/pay/payment")
@Slf4j
public class PayClientController {

  @Autowired
  private PayFactory payFactory;

  @Autowired
  private PaymentInfoService wxPaymentInfoServiceImpl;

  /**
   * 获取支付地址
   * @param paymentDto
   * @return
   */
  @PostMapping(value = "/getPayAddress")
  public String getPayAddress(@RequestBody PaymentDto paymentDto){
    //获取支付的bean
    PaymentInfoService paymentInfoService =
            payFactory.getPaymentInfoService(paymentDto.getPayway());
    //申请支付地址
    return paymentInfoService.getPayAddress(paymentDto);
  }

  /**
   * 主动查询支付结果
   * @param orderNo
   * @return
   */
  @GetMapping(value = "/getPayResult")
  public Boolean getPayResult(@RequestParam String orderNo,
                              @RequestParam String payway){
    //获取支付的bean
    PaymentInfoService paymentInfoService =
            payFactory.getPaymentInfoService(payway);
    //查询支付结果
    return paymentInfoService.getPayResult(orderNo);
  }

  /***
   * 微信服务器支付结果通知的接口
   * @param request
   * @return
   */
  // client/pay/payment/notify/address
  @RequestMapping(value = "/notify/address")
  @SneakyThrows
  public String wxNotifyAddress(HttpServletRequest request,
                                String orderNo){
           // //获取通知的内容
           // ServletInputStream is = request.getInputStream();
           // //定义输出流
           // ByteArrayOutputStream os = new ByteArrayOutputStream();
           // //定义缓冲区
           // byte[] buffer = new byte[1024];
           // //定义偏移量
           // int len = 0;
           // //读取和写入
           // while ((len = is.read(buffer)) != -1){
           //     os.write(buffer, 0, len);
           // }
           // //获取输出流的内容
           // String xmlString = new String(os.toByteArray());
           // //转map
           // Map<String, String> notifyResult = WXPayUtil.xmlToMap(xmlString);
           // System.out.println(notifyResult);
           //输出
    //        TODO-测试代码-----------开始
    String example = "{\"transaction_id\":\"4200002441202411136698233279\",\"nonce_str\":\"1a2b340ed33343389f45c3a2e2743593\",\"bank_type\":\"PAB_CREDIT\",\"openid\":\"oHwsHuIE5pkWXo64GtUN7drzqvHs\",\"sign\":\"269D079B88A45447B33D17BFA6D17239\",\"fee_type\":\"CNY\",\"mch_id\":\"1558950191\",\"cash_fee\":\"1\",\"out_trade_no\":\"7a3af7cf0a8a1d1481b2e16a9610086\",\"appid\":\"wx74862e0dfcf69954\",\"total_fee\":\"1\",\"trade_type\":\"NATIVE\",\"result_code\":\"SUCCESS\",\"time_end\":\"20241113135322\",\"attach\":\"{\\\"exchange\\\":\\\"payment_tingshu_order_exchange\\\",\\\"routingKey\\\":\\\"payment.order.tingshu\\\"}\",\"is_subscribe\":\"N\",\"return_code\":\"SUCCESS\"}";
    Map<String, String> notifyResult = JSONObject.parseObject(example, Map.class);
    notifyResult.put("out_trade_no", orderNo);
    //          TODO-测试代码----------结束
    //支付微服务后续处理
    wxPaymentInfoServiceImpl.afterPaid(notifyResult);
    //按照规范应答
    Map<String, String> response = new HashMap<>();
    response.put("return_code", "SUCCESS");
    response.put("return_msg", "OK");
    return WXPayUtil.mapToXml(response);
  }

}
